<template>
  <div class="home_winning">
    <div class="card_list_box">
      <ul class="card_title">
        <li class="title_left">
          <ImgT src="/imgs/home/zhongjiang.webp" alt="" />
          <span>{{ t("home_page_winning") }}</span>
        </li>
        <li class="title_right">
          <!-- <p class="more" @click="handleWinning(1)">
              更多
            </p> -->
          <div class="swiper-btn swiper_pre" :class="{ disabled: isWinningBeginning || winningData.length === 0 }"
            @click="otherHandlePrev(winningRef, 'winning')">
            <iconpark-icon name="zuo-keyong" size="24" />
          </div>
          <div class="swiper-btn swiper_next" :class="{ disabled: isWinningEnd || winningData.length === 0 }"
            @click="otherHandleNext(winningRef, 'winning')">
            <iconpark-icon name="you-keyong" size="24" />
          </div>
        </li>
      </ul>
      <swiper direction="horizontal" :modules="modules" :loop="false" :navigation="{
        prevEl: '.swiper_pre',
        nextEl: 'swiper_next',
      }" @swiper="(e: any) => { winningRef = e; updateSwiperStatus('winning'); }"
        @slideChange="updateSwiperStatus('winning')">
        <swiper-slide v-for="(item, i) in winningData" :key="i">
          <div class="winning_list">
            <ul class="winning_list_ul" v-for="(group, j) in item" :key="j">
              <li class="winning_list_li" @click.stop="handleWinning">
                <ImgT :src="`${settings.backend_upload}/head_icons/${group.head_icon}.webp`" alt="" />
                <span class="money">{{ formatBigNumber(group.money) }}</span>
                <span class="name">{{ group.nickname }}</span>
              </li>
            </ul>
          </div>
        </swiper-slide>
      </swiper>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, nextTick, watch, onBeforeMount, onUnmounted } from "vue";
import pinia from "@/store/index";
import { storeToRefs } from "pinia";
import { useI18n } from "vue-i18n";
import { NetPacket } from "@/netBase/NetPacket";
import { i18n } from "@/languages";
import { Swiper, SwiperSlide } from "swiper/vue";
import { Autoplay, Pagination } from "swiper/modules";

import "swiper/css";
import "swiper/css/pagination"; // 轮播图底面的小圆点
import "swiper/css/pagination";

import H5Websocket from "@/net/Net1";
import { MessageEvent2 } from "@/net/MessageEvent2";
import { NetMsgType } from "@/netBase/NetMsgType";

// import { convert2DisplayMoney } from "@/utils/useStoreMethods";
import { formatBigNumber } from "@/utils/publicMethods";
import { Page } from "@/store/page";
const { t } = useI18n();
const { lang, settings } = storeToRefs(Page(pinia));

const winningRef = ref(); //最近中奖
const winningData: any = ref([]); //最近中奖
const isWinningBeginning = ref(true);
const isWinningEnd = ref(false);

const handleRankList = async (res: any) => {
  console.log("最近中奖", res.room_rank_list);
  const chunks = chunkArray(res.room_rank_list, 5);
  winningData.value = chunks;
};
const chunkArray = (array: any, size: number) =>
  Array.from({ length: Math.ceil(array.length / size) }, (_, index) =>
    array.slice(index * size, index * size + size)
  );

const otherHandlePrev = (item: any, type: string) => {
  if (type === "winning" && !isWinningBeginning.value && winningData.value.length > 0) {
    item.slidePrev();
    updateSwiperStatus(type);
  }
};
const otherHandleNext = (item: any, type: string) => {
  if (type === "winning" && !isWinningEnd.value && winningData.value.length > 0) {
    item.slideNext();
    updateSwiperStatus(type);
  }
};
const updateSwiperStatus = (type: string) => {
  nextTick(() => {
    const swiper = type === "winning" ? winningRef.value : "";
    if (swiper) {
      if (type === "winning") {
        isWinningBeginning.value = swiper.isBeginning;
        isWinningEnd.value = swiper.isEnd;
      }
    }
  });
};

watch(winningData, () => {
  if (winningRef.value) {
    updateSwiperStatus("winning");
  }
});

const modules = [Autoplay, Pagination];

const handleWinning = () => {
  alert("敬请期待");
};

onBeforeMount(() => {
  i18n.global.locale.value = lang.value;

  //最近中奖
  const rankList = NetPacket.req_rank_with_robot_list();

  H5Websocket.sendMessageHandler(rankList);
  MessageEvent2.addMsgEvent(
    NetMsgType.msgType.msg_notify_rank_with_robot_list,
    handleRankList
  );
});

onUnmounted(() => {
  MessageEvent2.removeMsgEvent(NetMsgType.msgType.msg_notify_rank_with_robot_list, null);
});
</script>

<style lang="scss" scoped>
.home_winning {
  .card_list_box {
    padding-left: 14px;

    .card_title {
      display: flex;
      padding: 0 14px;
      justify-content: space-between;
      align-items: center;
      height: 24px;
      line-height: 24px;
      margin: 12px 0;

      .title_left {
        font-family: PingFang SC;
        font-size: 14px;
        font-weight: 600;
        line-height: 16px;
        text-align: left;
        color: #fcfffa;
        display: flex;
        justify-content: space-between;
        align-items: center;

        img {
          width: 18px;
          height: 18px;
          margin-right: 5px;
        }
      }

      .title_right {
        font-family: PingFang SC;
        font-size: 12px;
        font-weight: 500;
        line-height: 12px;
        text-align: left;
        color: #c6c4f5;
        display: flex;
        justify-content: space-between;
        align-items: center;

        .more {
          background: #282747;
          margin-left: 8px;
          padding: 8px;
          border-radius: 5px;
          box-shadow: 0.5px 0.5px 1px 0px #4b4a6e inset;
        }

        .swiper-btn {
          margin-left: 8px;
        }

        .disabled {
          pointer-events: none;
          opacity: 0.5;
          cursor: not-allowed;
        }
      }
    }

    .winning_list {
      display: flex;
      justify-content: flex-start;
      flex-wrap: wrap;

      .winning_list_ul {
        display: flex;
        // margin: 0 14px;
        margin-bottom: 10px;

        .winning_list_li {
          width: 62.5px;
          height: 96px;
          margin-right: 8px;
          border-radius: 8px;
          background: #131232;
          box-sizing: border-box;
          padding: 8px;
          box-shadow: 0.5px 0.5px 1px 0px #2e296b inset;
          display: flex;
          flex-direction: column;
          justify-content: space-around;
          align-items: center;

          img {
            width: 46px;
            height: auto;
            border-radius: 8px;
          }

          .money {
            font-family: SF Pro Display;
            font-size: 10px;
            font-weight: 700;
            line-height: 10px;
            text-align: left;
          }

          .name {
            width: 100%;
            font-family: PingFang SC;
            font-size: 10px;
            font-weight: 500;
            line-height: 10px;
            text-align: left;
            overflow: hidden;
          }
        }
      }
    }

    .masking_layer {
      width: 100%;
      height: 100%;
      position: absolute;
      top: 0;
      left: 0;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      backdrop-filter: blur(1px);
      background: rgba(0, 0, 0, 0.6);
      font-family: PingFang SC;
      font-size: 12px;
      font-weight: 500;
      line-height: 18px;
      color: #ffffff;

      .maskin_img {
        width: 32px;
        height: 32px;
        margin-bottom: 8px;
      }
    }
  }
}
</style>
